#include "ccolor.h"

/**
 * CColor - constructor, sets default color (white)
 */
CColor::CColor() {
  init(255, 255, 255);
}
//---------------------------------------------------------------------------

/**
 * CColor - constructor, sets color
 * @param r
 * @param g
 * @param b
 */
CColor::CColor(float r, float g, float b) {
  init((unsigned char) (r * 255), (unsigned char) (g * 255),
      (unsigned char) (b * 255));
}
//---------------------------------------------------------------------------

/**
 * CColor - constructor, sets color
 * @param r
 * @param g
 * @param b
 */
CColor::CColor(int r, int g, int b) {
  init((unsigned char) r, (unsigned char) g, (unsigned char) b);
}
//---------------------------------------------------------------------------

/**
 * CColor - constructor, sets color
 * @param r
 * @param g
 * @param b
 */
CColor::CColor(unsigned char r, unsigned char g, unsigned char b) {
  init(r, g, b);
}
//---------------------------------------------------------------------------

/**
 * CColor - constructor, sets color
 * @param hex
 */
CColor::CColor(int hex) {
  inHex(hex);
}
//---------------------------------------------------------------------------

/**
 * setR - sets red color factor
 * @param r
 */
void CColor::setR(unsigned char r) {
  this-> r = r;
}
//---------------------------------------------------------------------------

/**
 * setG - sets green color factor
 * @param g
 */
void CColor::setG(unsigned char g) {
  this-> g = g;
}
//---------------------------------------------------------------------------

/**
 * setB - sets blue color factor
 * @param b
 */
void CColor::setB(unsigned char b) {
  this-> b = b;
}
//---------------------------------------------------------------------------

/**
 * getR - returns red color factor
 * @return red color factor
 */
unsigned char CColor::getR() const {
  return r;
}
//---------------------------------------------------------------------------

/**
 * getG - returns green color factor
 * @return green color factor
 */
unsigned char CColor::getG() const {
  return g;
}
//---------------------------------------------------------------------------

/**
 * getB - returns blue color factor
 * @return blue color factor
 */
unsigned char CColor::getB() const {
  return b;
}
//---------------------------------------------------------------------------

/**
 * init - initialization
 * @param r
 * @param g
 * @param b
 */
void CColor::init(unsigned char r, unsigned char g, unsigned char b) {
  this-> r = r;
  this-> g = g;
  this-> b = b;
}
//---------------------------------------------------------------------------

/**
 * toString - prints color to string
 * @return string
 */
string CColor::toString() const {
  ostringstream stream;
  stream << "rgb(" << (short) r << ";" << (short) g << ";" << (short) b
      << ")";
  return stream.str();
}
//---------------------------------------------------------------------------

/**
 * inHex - saves color from hex
 * @param hex
 */
void CColor::inHex(int hex) {
  init((hex & 0xff0000) >> 16, (hex & 0x00ff00) >> 8, hex & 0x0000ff);
}
//---------------------------------------------------------------------------

/**
 * toHex - returns color in hex
 * @return hex
 */
int CColor::toHex() {
  return toHex(*this);
}
//---------------------------------------------------------------------------

/**
 * toGrayscale - returns color value in grayscale
 * @return grayscale
 */
CColor CColor::toGrayscale() {
  return toGrayscale(*this);
}
//---------------------------------------------------------------------------

/**
 * toHex - converts RGB into hex
 * @param dec
 * @return hex
 */
int CColor::toHex(CColor dec) {
  return (dec.r << 16) + (dec.g << 8) + dec.b;
}
//---------------------------------------------------------------------------

/**
 * toGrayscale - converts RGB into grayscale
 * @param rgb
 * @return grayscale
 */
CColor CColor::toGrayscale(CColor rgb) {
  unsigned char tmp = (unsigned char) (rgb.r * 0.3 + rgb.g * 0.59 + rgb.b
      * 0.11);
  CColor gray(tmp, tmp, tmp);
  return gray;
}
//---------------------------------------------------------------------------
